package cn.edu.zjut.service.impl;

import cn.edu.zjut.entity.UserEntity;
import cn.edu.zjut.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import cn.edu.zjut.service.IUserService;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author:ZhouDan
 * @date: 2020/5/24
 * @version: 1.0
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        return userRepository.findUserEntityByUserName(s);
    }

    @Override
    public UserEntity getByUserName(String userName) {
        return userRepository.findUserEntityByUserName(userName);
    }

    @Override
    public UserEntity getByTelephone(String telephone) {
        return userRepository.findByTelephone(telephone);
    }

    @Override
    public UserEntity getByUserNameAndTelephone(String userName, String telephone) {
        return userRepository.findByUserNameAndTelephone(userName,telephone);
    }

    @Override
    public UserEntity login(String userName, String password) {
        UserEntity user = userRepository.findByUserNameAndPassword(userName,password);
        if(user == null){
            return null;
        }
        return user;
    }

    @Override
    public UserEntity getById(long id) {
        return userRepository.findOne(id);
    }

    @Override
    public UserEntity save(UserEntity userEntity) {
        return userRepository.save(userEntity);
    }
}
